package com.ptszyxx.popose.common.utils;

import android.content.Context;
import android.text.TextUtils;

import com.ptszyxx.popose.common.constants.AppConstant;
import com.tencent.bugly.crashreport.CrashReport;
import com.ysg.base.BaseApplication;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * bugly:专业闪退收集库
 */
public class YCrashUtil {
    /**
     * 初始化
     */
    public static void init(){
        Context context = BaseApplication.getContext();
        String packageName = context.getPackageName();
        String processName = getProcessName(android.os.Process.myPid());
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
         CrashReport.initCrashReport(context, AppConstant.APP_BUGLY_ID, true, strategy);
        //上传额外参数
//        String userCode = SharedPreferencesUtils.getString(context, BaseConstants.USER_CODE, "");
//        if (YStringUtil.isNotEmpty(userCode)){
//            CrashReport.setUserId(userCode);
//            CrashReport.setDeviceModel(context, YPhoneUtil.getModel());
//        }
    }


    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    public static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }



}
